<template>
	<view>
		<view class="inp">
			<input v-model="model" type="text">
		</view>
		<view v-if="flag" class="all">
			<view class="his">
				<view>历史搜索</view>
				<view @click="funs">删除</view>
			</view>
			<view class="qqq">
				<view class="" v-for="(item,index) in hislist">
					{{item}}
				</view>
			</view>
		</view>
		<view class="all" v-else>
			<view v-if="datalist != ''">
				<view class="nowp" v-for="item in datalist">
					{{item.goods_name}}
				</view>
			</view>
			<view v-else style="text-align: center;margin-top: 100rpx;">
				暂无数据
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hislist:['aaa','bbb','ccc','ddd'],
				datalist:[],
				time:null,
				flag:true,
				model:""
			};
		},
		onLoad() {
			let arr = uni.getStorageSync('arr')
			this.hislist = arr
			
		},
		methods:{
			fun(str){
				if(this.hislist == ""){
					this.hislist = []
					this.hislist.push(str)
					uni.setStorageSync('arr', this.hislist);
				}else{
					this.hislist.push(str)
					uni.setStorageSync('arr', this.hislist);
					const set = new Set(this.hislist)
					set.delete(str)
					this.hislist = Array.from(set)
					this.hislist.unshift(str)
				}
				uni.request({
					url:"https://www.uinav.com/api/public/v1/goods/qsearch",
					data:{query:str},
				    success: (res) => {
						this.datalist = res.data.message;
						console.log(res)
					}
				})
			},
			funs(){
				uni.removeStorageSync('arr')
				this.hislist = []
			}
			
		},
		watch:{
			model(n){
				if(n == ""){
					this.flag = true
					return
				}else{
					this.flag = false
				}
				clearTimeout(this.time)
				this.time = setTimeout(() => {
					this.time = null
					this.fun(n)
				},500)
			}
		}
	}
</script>

<style lang="scss">
	.inp{
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: gainsboro;
		position: fixed;
		top: 0px;
		input{
			width: 90%;
			height: 80%;
			background-color: white;
			margin: 0 5%;
			position: relative;
			top: 10%;
			border-radius: 10px;
			padding: 20rpx;
			box-sizing: border-box;
		}
	}
	.his{
		display: flex;
		justify-content: space-between;
	}
	.all{
		margin-top: 100rpx;
		padding: 10px 20px;
	}
	.qqq{
		display:flex;
		flex-wrap: wrap;
		>view{
			background-color: gainsboro;
			margin-right: 20px;
			margin-top: 20px;
			padding: 10px;
			border-radius: 10px;
		}
	}
	.nowp{
		overflow: hidden;
		white-space:nowrap;
		text-overflow: ellipsis;
		margin: 20px 0;
	}
</style>
